package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.impl.UserRedisServiceImpl;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.repository.UserInfoRepsitory;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepsitory userInfoRepsitory;

    @Autowired
    private UserRedisServiceImpl userRedisService;

    @Override
    public void save(UserInfo userInfo) {
        userInfoRepsitory.save(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoRepsitory.save(userInfo);
    }

    @Override
    public void delete(String id) {
        userInfoRepsitory.deleteById(id);
    }

    @Override
    public UserInfo get(String id) {
        return userInfoRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return userInfoRepsitory.findAll();
    }

    //判断手机号码是否存在
    @Override
    public boolean checkPhone(String phone) {
        UserInfo userInfo = userInfoRepsitory.findByPhone(phone);
        return userInfo != null;
    }

    //发送短信验证码
    @Override
    public void sendVerifyCode(String phone) {
        //创建验证码,replace替换掉验证码中的-,substring截取前4位数
        String code = UUID.randomUUID().toString().replace("-", "")
                .substring(0, 4);

        //创建短信
        StringBuilder sb = new StringBuilder(80);
        sb.append("你注册的短信验证码是:").append(code).append(",请在")
                .append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内验证");

        //假装发送短信
        System.out.println(sb);

        //把短信和验证码存入redis中
        userRedisService.setVerifyCode(phone, code);
    }

    //用户注册
    @Override
    public void verifyCode(String phone, String nickname,
                           String password, String rpassword, String verifyCode) {
        //校验参数是否为空
        AssertUtil.hasLength(phone, "手机号码不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");
        //校验两次输入密码是否一致
        AssertUtil.isEqual(password, rpassword, "密码不一致");
        //校验手机号码是否正确 @TODO Java 的正则表达式
        if (!phone.matches("1[3-9][0-9]{9}")) {
            throw new LogicException("手机号码格式不对");
        }
        //校验手机号码是否唯一
        if (this.checkPhone(phone)) {
            throw new LogicException("该号码已注册");
        }
        //校验短信验证码是否正确
        String code = userRedisService.getVerifyCode(phone);
        if (!code.equalsIgnoreCase(verifyCode)) {
            throw new LogicException("验证码不正确");
        }
        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setPassword(password);
        userInfo.setLevel(1);
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");
        //核心属性必须自己控制
        userInfo.setState(UserInfo.STATE_NORMAL);
        //保存
        this.save(userInfo);
    }

    //验证用户账号密码
    @Override
    public UserInfo login(String username, String password) {
        UserInfo userInfo = userInfoRepsitory.findByPhone(username);
        if (userInfo == null || !password.equals(userInfo.getPassword())) {
            throw new LogicException("账号或密码错误");
        }
        userInfo.setPassword("");
        return userInfo;
    }

    //查询目的地下用户
    @Override
    public List<UserInfo> queryCity(String city) {
        return userInfoRepsitory.findByCity(city);
    }
}

